package com.wwb.w0503;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
Button button,button2,send;
EditText editText,editText2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=findViewById(R.id.button);
        editText=findViewById(R.id.tel);
        button2=findViewById(R.id.getmessage);
        editText2=findViewById(R.id.mtemplate);
        send=findViewById(R.id.send);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent();
                intent.setAction("contacts");
                intent.addCategory("android.intent.category.DEFAULT" );

                startActivityForResult(intent,1);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("message");
                intent.addCategory("android.intent.category.DEFAULT" );

                startActivityForResult(intent,20);
            }
        });

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String sms = editText2.getText().toString();
                String tel = editText.getText().toString();
                Intent intent=new Intent(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("smsto:"+tel));
                intent.putExtra("sms_body",sms);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==2){
            String telneed=data.getStringExtra("tel").toString();
            editText.setText(telneed);
        }
        if(requestCode==20&&resultCode==21){
            String messageneed=data.getStringExtra("message").toString();
            editText2.setText(messageneed);
        }
    }



}